import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 员工维护与管理
 * @date: 2018-6-19


 */
import { getResponse, createPagination } from 'utils/utils';
import { searchPositionInfo, searchAddibleStaff, searchAddedStaff, deleteStaff as _deleteStaff, addStaff as _addStaff } from "../services/staffService";
export default {
  namespace: 'staff',
  state: {
    addedStaff: {
      // 已添加的员工
      list: [],
      pagination: {}
    },
    addibleStaff: {
      // 可添加的员工
      list: [],
      pagination: {}
    },
    positionCode: '',
    positionName: '',
    unitId: ''
  },
  effects: {
    // 获取岗位信息
    fetchPositionInfo: /*#__PURE__*/_regeneratorRuntime.mark(function fetchPositionInfo(_ref, _ref2) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchPositionInfo$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(searchPositionInfo, payload);

            case 5:
              _context.t1 = _context.sent;
              result = (0, _context.t0)(_context.t1);

              if (!result) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  positionCode: result.positionCode,
                  positionName: result.positionName,
                  unitId: result.unitId
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, fetchPositionInfo);
    }),
    // 获取岗位可已添加的员工列表
    fetchAddibleStaff: /*#__PURE__*/_regeneratorRuntime.mark(function fetchAddibleStaff(_ref3, _ref4) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchAddibleStaff$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(searchAddibleStaff, payload);

            case 4:
              result = _context2.sent;
              result = getResponse(result);

              if (!result) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  addibleStaff: {
                    list: result.content,
                    pagination: createPagination(result)
                  }
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchAddibleStaff);
    }),
    // 获取岗位已添加的员工列表
    fetchAddedStaff: /*#__PURE__*/_regeneratorRuntime.mark(function fetchAddedStaff(_ref5, _ref6) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchAddedStaff$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context3.next = 4;
              return call(searchAddedStaff, payload);

            case 4:
              result = _context3.sent;
              result = getResponse(result);

              if (!result) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  addedStaff: {
                    list: result.content,
                    pagination: createPagination(result)
                  }
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchAddedStaff);
    }),
    // 删除员工信息
    deleteStaff: /*#__PURE__*/_regeneratorRuntime.mark(function deleteStaff(_ref7, _ref8) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteStaff$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_deleteStaff, payload);

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, deleteStaff);
    }),
    // 添加员工信息
    addStaff: /*#__PURE__*/_regeneratorRuntime.mark(function addStaff(_ref9, _ref10) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function addStaff$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_addStaff, payload);

            case 4:
              result = _context5.sent;
              return _context5.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, addStaff);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref11) {
      var payload = _ref11.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};